package org.jypj.dev.controller;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jypj.dev.dao.IGiveWorkDao;
import org.jypj.dev.model.GiveWork;
import org.jypj.dev.util.ExcelHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 派班导出
 */
@Controller
@RequestMapping("giveWorkExport")
public class GiveWorkExportController extends BaseController {

    @Resource
    private IGiveWorkDao giveWorkDao;

    /**
     * 查询所有派班并导出
     *
     * @param startDate
     */
    @RequestMapping("exportGiveWorks.action")
    public void exportGiveWorks(String startDate) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("workDate", startDate);
        List<GiveWork> giveWorks = giveWorkDao.selectAllByMap(queryMap);
        if (giveWorks != null) {
            exportExcel(giveWorks, startDate + " excel", getRequest().getSession(), getResponse());
        }
    }


    /**
     * 导出excel
     *
     * @param giveWorks
     * @param title
     * @param session
     * @param response
     */
    public void exportExcel(List<GiveWork> giveWorks, String title, HttpSession session,
                            HttpServletResponse response) {

        WritableWorkbook workbook_temp = null;// Excel数据组装
        String usersTempPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "giveWorkRecord_temp.xls";
        String userPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "giveWorkRecord.xls";
        Workbook workbook = null; // 工作表
        try {
            workbook = Workbook.getWorkbook(new File(userPath)); // 创建一张工作表从实体路径下
            workbook_temp = Workbook.createWorkbook(new File(usersTempPath), workbook); // 拷贝一份模板
            WritableSheet sheet = workbook_temp.getSheet(0);
            for (int i = 0; i < giveWorks.size(); i++) {
                GiveWork giveWork = giveWorks.get(i);
                sheet.addCell(new Label(0, 1 + i, i + 1 + ""));
                sheet.addCell(new Label(1, 1 + i, giveWork.getFirstTrainCode()));
                sheet.addCell(new Label(2, 1 + i, giveWork.getTrainNum()));
                sheet.addCell(new Label(3, 1 + i, giveWork.getUserName()));
                sheet.addCell(new Label(4, 1 + i, giveWork.getRemindTime()));
                sheet.addCell(new Label(5, 1 + i, giveWork.getWorkDate()));
                sheet.addCell(new Label(6, 1 + i, giveWork.getRemindContent()));
                sheet.addCell(new Label(7, 1 + i, "1".equals(giveWork.getHadRemind()) ? "是" : "否"));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭Excel工作薄对象
            if (workbook_temp != null) {
                try {
                    workbook_temp.write();
                    workbook_temp.close();
                    ExcelHelper.outputExcel(response, title, usersTempPath);
                    new File(usersTempPath).delete();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (workbook != null) {
                workbook.close();
            }
        }

    }
}
